/*  ----------------------------------------------------------------------------
 *  Project Intercept
 *  ----------------------------------------------------------------------------
 *  System.Common
 *  ----------------------------------------------------------------------------
 *  File:        Integers.cs
 *  Authors:     Vendetta
 *  ----------------------------------------------------------------------------
 *  GNU General Public License version 2 (GPLv2)
 */
namespace System.Common
{
    public static class Integers
    {
        /// <summary>
        /// Checks to see if the specified Long is prime.
        /// </summary>
        /// <param name="ToBeChecked">The Int64/Long to be checked for prime.</param>
        /// <returns>Returns true if the long is prime, false if it is not prime.</returns>
        public static bool IsPrime(long ToBeChecked)
        {
            //We start with 2 because every number from 1 to infinity is divisible by 1
            for (long i = 2; i < ToBeChecked; i++)
            {
                if (ToBeChecked % i == 0)
                    return false;
            }
            return true;
        }
        /// <summary>
        /// Checks to see if a number is within an Int32/int's maximum value
        /// </summary>
        /// <param name="NumberinQuestion">The number in a string in question</param>
        /// <returns>Returns true if the numberInQuestion can fit in an Int32's maximum value , false otherwise</returns>
        public static bool IsInt32(string NumberinQuestion)
        {
            long l; Int64.TryParse(NumberinQuestion, out l);
            if (l <= Int32.MaxValue)
                return true;

            return false;
        }

        /// <summary>
        /// Checks to see if a number is within an Int16's maximum value 
        /// </summary>
        /// <param name="NumberinQuestion">The string of a number in question</param>
        /// <returns>Returns true if the numberInQuestion is within an Int16's maximum value, false otherwise</returns>
        public static bool IsInt16(string NumberinQuestion)
        {
            long l; Int64.TryParse(NumberinQuestion, out l);
            if (l <= Int16.MaxValue)
                return true;

            return false;
        }

        /// <summary>
        /// Checks to see if the specified string of a number is within a Long/Int64's maximum value
        /// </summary>
        /// <param name="NumberinQuestion">The string of the number in question</param>
        /// <returns>Returns true if the number fits in a long/Int64 data type, false if it is too large.</returns>
        public static bool IsInt64(string NumberinQuestion)
        {
            long l; Int64.TryParse(NumberinQuestion, out l);
            if (l <= Int64.MaxValue)
                return true;

            return false;
        }

        /// <summary>
        /// Converts a specified Integer to Hex
        /// </summary>
        /// <param name="Integer">The Integer you wish to convert to Hex</param>
        /// <returns>a String containing the Hex of the Integer</returns>
        public static string IntToHex(int Integer)
        {
            return Integer.ToString("X");
        }
        /// <summary>
        /// Converts a specified Hex string to an Integer
        /// </summary>
        /// <param name="Hex">The string containing the Hex value you wish to convert</param>
        /// <returns>Returns an integer of the hex value, or -1 if the conversion fails</returns>
        public static int HexToInt(string Hex)
        {
            try
            {
                return int.Parse(Hex, System.Globalization.NumberStyles.HexNumber);
            }
            catch { return -1; }
        }

    }
}